package cn.xopencode.appletweb.controller.record.drytransformer;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.drytransformer.DryTransformerRecordRpc;
import cn.xopencode.recordservice.rpc.drytransformer.dto.DryTransformerRecordCreateDTO;
import cn.xopencode.recordservice.rpc.drytransformer.dto.DryTransformerRecordPageDTO;
import cn.xopencode.recordservice.rpc.drytransformer.dto.DryTransformerRecordUpdateDTO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordEmpRelVO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordPageVO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordUpdateVO;
import cn.xopencode.recordservice.rpc.robotroom.dto.AdminRecordPageDTO;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import cn.xopencode.userservice.rpc.user.UserRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "10KV干式变压器巡检记录 API")
@RestController
@RequestMapping("/drytransformer")
@Validated
public class DryTransformerRecordController {

    @DubboReference(version = "${dubbo.consumer.DryTransformerRecordRpc.version}")
    private DryTransformerRecordRpc dryTransformerRecordRpc;
    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;
    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;
    /**
     * 根据查询条件 分页获取所有的10KV干式变压器巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的10KV干式变压器巡检记录")
    public CommonResult<PageResult<DryTransformerRecordPageVO>> getPageDryTransformerRecord(DryTransformerRecordPageDTO pageDTO){
        CommonResult<PageResult<DryTransformerRecordPageVO>> result = dryTransformerRecordRpc.pageDryTransformerRecord(pageDTO);
        List<DryTransformerRecordPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIds())){
                pageVO.setEmpRels(userRpc.getEmployeeName(pageVO.getEmpIds()));
            }
        });
        return result;
    }

    /**
     *  新增 10KV干式变压器巡检记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增 10KV干式变压器巡检记录")
    public CommonResult<Integer> createDryTransformerRecord(@Valid DryTransformerRecordCreateDTO roomDTO){
        return dryTransformerRecordRpc.createDryTransformerRecord(roomDTO);
    }

    /**
     *  更新 10KV干式变压器巡检记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新 10KV干式变压器巡检记录")
    public CommonResult<Integer> updateDryTranformerRecord(@Valid DryTransformerRecordUpdateDTO roomDTO){
        return dryTransformerRecordRpc.updateDryTransformerRecord(roomDTO);
    }

    /**
     * 根据id获取10KV干式变压器巡检记录详细
     * @param roomId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取10KV干式变压器巡检记录详细")
    public CommonResult<DryTransformerRecordUpdateVO> getDryTranformerRecordById(String roomId){
        CommonResult<DryTransformerRecordUpdateVO> result = dryTransformerRecordRpc.getDryTransformerRecordById(roomId);
        DryTransformerRecordUpdateVO updateVO = result.getData();
        if(updateVO != null){
            List<String> list = updateVO.getEmpList().stream().map(DryTransformerRecordEmpRelVO::getEmpId).collect(Collectors.toList());
            String empNames = userRpc.getEmployeeName(StringUtils.join(list, ","));
            updateVO.setEmpNames(empNames);
        }
        return result;
    }

    /**
     * 处理 10KV干式变压器巡检记录 中的异常，在巡检记录中只是改变状态
     * @param recordId
     * @return
     */
    @PostMapping("/deal")
    @ApiOperation("处理 10KV干式变压器巡检记录 中的异常")
    public CommonResult<Integer> dealDryTranformerRecordException(@RequestParam(required = true) String recordId){
        return dryTransformerRecordRpc.dealDryTranformerRecordException(recordId);
    }

    /**
     * 后台--根据查询条件 分页获取所有的10KV干式变压器巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllAdminByQuery")
    @ApiOperation("后台--根据查询条件分页获取所有的10KV干式变压器巡检记录")
    public CommonResult<PageResult<DryTransformerRecordAdminPageVO>> getAdminPageDryTranformerRecord(AdminRecordPageDTO pageDTO){
        String level2DeptId = pageDTO.getLevel2DeptId();
        String deptId = pageDTO.getDeptId();
        if(!StringUtils.isEmpty(level2DeptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(level2DeptId).getData());
        }else if(!StringUtils.isEmpty(deptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(deptId).getData());
        }
        CommonResult<PageResult<DryTransformerRecordAdminPageVO>> result = dryTransformerRecordRpc.getAdminPageDryTranformerRecord(pageDTO);
        List<DryTransformerRecordAdminPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIds()))
                pageVO.setEmpNames(userRpc.getEmployeeName(pageVO.getEmpIds()));
        });
        return result;
    }
}
